package com.maxlen.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxlen.admin.entity.Permission;
import com.maxlen.admin.entity.dto.PageParamDto;
import com.maxlen.admin.entity.dto.PermissionDto;
import com.maxlen.admin.mapper.PermissionMapper;
import com.maxlen.admin.service.IPermissionService;
import com.maxlen.admin.service.IRolePermissionService;
import com.maxlen.admin.utils.WrapperUtils;
import com.maxlen.common.utils.CopyUtils;
import com.maxlen.common.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-10-11
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    private IRolePermissionService solePermissionService;

    @Override
    public List<String> findAllPathByRoleId(Long id) {
        return baseMapper.findAllPathByRoleId(id);
    }

    @Override
    public Page<Permission> findAllByPage(PageParamDto pageParamDto) {
        Page<Permission> page = new Page<>(pageParamDto.getCurrentPage(), pageParamDto.getPageSize());
        return baseMapper.selectPage(page, WrapperUtils.getWrapper(pageParamDto, Permission.class));
    }

    @Override
    public boolean add(PermissionDto permissionDto) {
        Permission permission = CopyUtils.copy(permissionDto, Permission.class);
        int insert = baseMapper.insert(permission);
        if(insert > 0){
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean deleteById(Long id) {
        int deleteById = baseMapper.deleteById(id);
        if(deleteById > 0){
            solePermissionService.removeByPermissionId(id);
            return true;
        }
        return false;
    }

    @Override
    public boolean updateById(PermissionDto permissionDto) {
        Permission permission = CopyUtils.copy(permissionDto, Permission.class);
        int update = baseMapper.updateById(permission);
        if(update > 0){
            return true;
        }
        return false;
    }

    @Override
    public List<String> findPathByRequest(HttpServletRequest request) {
        String jwtToken = request.getHeader("token");
        if(StringUtils.hasText(jwtToken)){
            String id = JwtUtils.getMemberIdByJwtToken(jwtToken);
            if(StringUtils.hasText(id)){
                return baseMapper.findAllPathByAdminId(Long.valueOf(id));
            }
        }
        return null;
    }

    @Override
    public List<Permission> findListByRoleId(Long id) {
        return baseMapper.findAllListByRoleId(id);
    }

    @Override
    @Transactional
    public boolean deleteByIdList(List<Long> multipleId) {
        for (Long num : multipleId) {
            if(!deleteById(num)){
                return false;
            }
        }
        return true;
    }
}
